﻿using System;
using System.Windows.Forms;
using ChatClientApp.Classes;
using System.Collections.Generic;
using ChatServerApp.Classes;

namespace ChatClientApp.GUI
{
    public partial class fContactSearch : Form
    {
        public fContactSearch()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == Tools.ActiveComponents.ActiveUser.Nick)
            {
                MessageBox.Show("This is your Nick...");
                return;
            }
            else if (Tools.ActiveComponents.ActiveUser.Contacts.Contains(textBox1.Text))
            {
                MessageBox.Show("Already in your ContactList...");
                return;
            }

            List<User> lou = ServerHandling.SearchUser(textBox1.Text);
            if (lou.Count > 0)
            {
                foreach (User u in lou)
                {
                    listBox1.Items.Add(u.Nick);
                }
            }
            else
            {
                MessageBox.Show("User '" + textBox1.Text + "' not found.");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                listBox2.Items.Add(listBox1.SelectedItem);
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex != -1)
            {
                listBox1.Items.Add(listBox2.SelectedItem);
                listBox2.Items.RemoveAt(listBox2.SelectedIndex);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count == 0)
            {
                MessageBox.Show("No Contacts marked...");
            }
            else
            {
                List<string> listOfUsers = new List<string>();
                for (int i = 0; i < listBox2.Items.Count; i++ )
                {
                    listOfUsers.Add(listBox2.Items[i].ToString());
                    Tools.ActiveComponents.ActiveUser.Contacts.Add(listBox2.Items[i].ToString());
                }
                ServerHandling.AddContacts(Tools.ActiveComponents.ActiveUser.Nick, listOfUsers);
                this.Close();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
